<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Event\DBEvent;
		class IndexController extends Controller {

	public function index(){
		$this->assign('title','系统概况');
		$this->assign('index','1');
		$this->display('');
	}

	public function loginpage(){
		layout(false);
		$this->display('');
	}


	public function search(){
		$this->assign('title','搜索采集');
		$this->assign('index','3');
		$this->display('');
	}
	public function answer(){
		$this->assign('title','问答采集');
		$this->assign('index','2');
		$this->display('');
	}
	public function warning(){
		$this->assign('title','预警提醒');
		$this->assign('index','4');
		$this->display('');
	}
    public function login(){
    	DBEvent::createTableAndRule();
    	$where['name'] = I('param.name','');
    	$where['pwd'] = I('param.pwd','');
    	$userTable = D('User');
    	if($userTable->create($where)){
    		$lockUser = $userTable->where(array('lock'=>0,'name'=>$where['name']))->select();
    		if($lockUser){
    			echo json_encode(array('result'=>I('param.name','').'已被锁定，请联系管理员','code'=>0));
    		}else{
    			$user = $userTable->where($where)->select();
    			if($user){
    				$data['ip'] = get_client_ip();
    				$map['id'] = $user[0]['id'];
    				$userTable->where($map)->save($data);
    				echo json_encode(array('result'=>'登录成功','code'=>1));
    				session('name',$user[0]['name']);
    				session('uid',$user[0]['id']);
    				session('email',$user[0]['email']);
    			}else{
    				echo json_encode(array('result'=>'用户名或密码错误','code'=>0));
    			}
    		}
    		
    	}else{
    		echo json_encode(array('result'=>$userTable->getError(),'code'=>2));
    	}    
    }
    
    //登出和查看是否登录,0登出，1，查看
    public function loginOperation(){
    	$flag = I('param.flag',0);
    	if($flag){
    		if(session('name')){
    			echo json_encode(array('result'=>array('uid'=>session('uid'),'name'=>session('name'),'email'=>session('email')),'code'=>1));
    		}else{
    			echo json_encode(array('result'=>'没有用户登录','code'=>0));
    		}
    	}else{
    		session('name',null);
    		session('id',null);
    		echo json_encode(array('result'=>'登出成功','code'=>1));
    	}
    }
   

    
    

}